using System;
using System.Windows.Forms;

namespace demo
{
    /// <summary>
    /// UsbStorageController使用示例
    /// 展示如何在Form1或其他窗体中使用USB存储设备控制功能
    /// </summary>
    public partial class UsbStorageControllerUsageExample
    {
        private UsbStorageController usbController;

        public UsbStorageControllerUsageExample()
        {
            // 初始化USB控制器
            usbController = new UsbStorageController();
        }

        /// <summary>
        /// 示例：检查管理员权限并处理
        /// </summary>
        public void CheckAdminPermissions()
        {
            if (!usbController.IsAdministrator())
            {
                DialogResult result = MessageBox.Show(
                    "此操作需要管理员权限。\n是否以管理员身份重启程序？",
                    "权限请求",
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Question);

                if (result == DialogResult.Yes)
                {
                    var restartResult = usbController.RestartAsAdministrator();
                    if (!restartResult.Success)
                    {
                        MessageBox.Show(restartResult.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            else
            {
                MessageBox.Show("当前程序已具有管理员权限。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        /// <summary>
        /// 示例：获取并显示USB设备状态
        /// </summary>
        public void ShowUsbDeviceStatus()
        {
            UsbDeviceStatus status = usbController.GetUsbStorageDeviceStatus();
            string statusText = "";

            switch (status)
            {
                case UsbDeviceStatus.Enabled:
                    statusText = "USB存储设备当前状态：已启用";
                    break;
                case UsbDeviceStatus.Disabled:
                    statusText = "USB存储设备当前状态：已禁用";
                    break;
                case UsbDeviceStatus.Unknown:
                    statusText = "USB存储设备当前状态：未知";
                    break;
            }

            MessageBox.Show(statusText, "USB设备状态", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        /// <summary>
        /// 示例：完整的USB设备管理流程
        /// </summary>
        public void ManageUsbDevices()
        {
            // 1. 检查管理员权限
            if (!usbController.IsAdministrator())
            {
                MessageBox.Show("需要管理员权限才能管理USB设备。", "权限不足", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            // 2. 获取当前状态
            UsbDeviceStatus currentStatus = usbController.GetUsbStorageDeviceStatus();

            // 3. 根据当前状态提供操作选项
            string message = "";
            string action = "";

            switch (currentStatus)
            {
                case UsbDeviceStatus.Enabled:
                    message = "USB存储设备当前已启用。\n是否要禁用USB存储设备？";
                    action = "disable";
                    break;
                case UsbDeviceStatus.Disabled:
                    message = "USB存储设备当前已禁用。\n是否要启用USB存储设备？";
                    action = "enable";
                    break;
                case UsbDeviceStatus.Unknown:
                    message = "无法确定USB存储设备状态。\n请选择要执行的操作：";
                    // 对于未知状态，可以提供更多选项
                    break;
            }

            if (currentStatus != UsbDeviceStatus.Unknown)
            {
                DialogResult result = MessageBox.Show(message, "USB设备管理", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

                if (result == DialogResult.Yes)
                {
                    UsbOperationResult operationResult;

                    if (action == "disable")
                    {
                        operationResult = usbController.DisableUsbStorageDevices();
                    }
                    else
                    {
                        operationResult = usbController.EnableUsbStorageDevices();
                    }

                    // 显示操作结果
                    MessageBoxIcon icon = operationResult.Success ? MessageBoxIcon.Information : MessageBoxIcon.Error;
                    MessageBox.Show(operationResult.Message, "操作结果", MessageBoxButtons.OK, icon);
                }
            }
            else
            {
                // 对于未知状态，显示选择对话框
                ShowUsbManagementDialog();
            }
        }

        /// <summary>
        /// 显示USB管理对话框
        /// </summary>
        private void ShowUsbManagementDialog()
        {
            DialogResult result = MessageBox.Show(
                "请选择要执行的操作：\n\n" +
                "是(Y) - 启用USB存储设备\n" +
                "否(N) - 禁用USB存储设备\n" +
                "取消 - 不执行任何操作",
                "USB设备管理",
                MessageBoxButtons.YesNoCancel,
                MessageBoxIcon.Question);

            UsbOperationResult operationResult = null;

            switch (result)
            {
                case DialogResult.Yes:
                    operationResult = usbController.EnableUsbStorageDevices();
                    break;
                case DialogResult.No:
                    operationResult = usbController.DisableUsbStorageDevices();
                    break;
                case DialogResult.Cancel:
                    return; // 用户取消操作
            }

            if (operationResult != null)
            {
                MessageBoxIcon icon = operationResult.Success ? MessageBoxIcon.Information : MessageBoxIcon.Error;
                MessageBox.Show(operationResult.Message, "操作结果", MessageBoxButtons.OK, icon);
            }
        }
    }
}